package com.example.demo.hooks;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Slf4j
@Service
@RequiredArgsConstructor
public class SpringHooksCloseListener implements ApplicationListener<ContextClosedEvent> {

    private final Searcher searcher;

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        try {
            //关闭资源 - 该 searcher 对象可以安全用于并发，等整个服务关闭的时候再关闭 searcher
            searcher.close();
        } catch (IOException e) {
            log.error("searcher对象关闭失败");
            throw new RuntimeException(e);
        }
    }
}
